package com.itheima;

import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/updateServlet")
public class UpdateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//       获取所有参数
        Map<String, String[]> map = request.getParameterMap();
        //使用工具类封装成对象
//        String id = request.getParameter("id");
//        int i = Integer.parseInt(id);
        User u=new User();
        try {
            BeanUtils.populate(u,map);
        } catch (Exception e) {}
        //调用集合找到对应的对象进行修改
        UserDao dao=new UserDao();

        ServletContext servletContext = request.getServletContext();
        User user = (User)servletContext.getAttribute("user");
        int id1 = user.getId();
        dao.updateUser(id1,u);
        //转发到展示页面
        request.getRequestDispatcher("/findServlet").forward(request,response);



    }
}
